{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-11-01 20:07:59.984315: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
      "2021-11-01 20:07:59.984370: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential      #线性模型框架\n",
    "from keras.layers import Dense, Dropout, Flatten   #Dense全连接层; Flatten平铺层\n",
    "from keras.layers import Conv2D, MaxPooling2D   #Conv2D 二维卷积层; MaxPooling2D: 二维池化层"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 准备数据:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x_train_0, y_train_0), (x_test_0, y_test_0) = mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_train=x_train_0.shape[0]\n",
    "n_test=x_test_0.shape[0]\n",
    "width,height=x_train_0.shape[1], x_train_0.shape[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 28, 28)\n",
      "(60000, 28, 28, 1)\n"
     ]
    }
   ],
   "source": [
    "x_train=x_train_0.reshape(n_train, width, height, 1)\n",
    "x_test=x_test_0.reshape(n_test, width, height, 1)\n",
    "print(x_train_0.shape)\n",
    "print(x_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.utils import np_utils\n",
    "y_train=np_utils.to_categorical(y_train_0)\n",
    "y_test=np_utils.to_categorical(y_test_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 10)\n",
      "[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n",
      "(60000,)\n",
      "处理前的最大值为255.000000\n"
     ]
    }
   ],
   "source": [
    "print(y_train.shape)\n",
    "print(y_train[0])\n",
    "print(y_train_0.shape)\n",
    "print(\"处理前的最大值为%f\" % x_train.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "处理后的最大值为1.000000\n"
     ]
    }
   ],
   "source": [
    "x_train=x_train/255\n",
    "x_test=x_test/255\n",
    "print(\"处理后的最大值为%f\" % x_train.max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 构造模型:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-11-01 20:08:01.956652: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory\n",
      "2021-11-01 20:08:01.956700: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)\n",
      "2021-11-01 20:08:01.956730: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (nlps-team-01): /proc/driver/nvidia/version does not exist\n",
      "2021-11-01 20:08:01.957044: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    }
   ],
   "source": [
    "#keras中的线性模型\n",
    "model=Sequential()  \n",
    "#二维卷积层, 32个5X5的卷积核, 使用ReLU作为激活函数\n",
    "model.add(Conv2D(32,(5,5),activation=\"relu\",input_shape=(width,height,1))) \n",
    "#最大池化层: 2X2大小的池化核\n",
    "model.add(MaxPooling2D(pool_size=(2,2))) \n",
    "#二维卷积层: 32个3x3的卷积核,使用ReLU作为激活函数\n",
    "model.add(Conv2D(32, (3,3), activation=\"relu\"))  \n",
    "#最大池化层: 2X2大小的池化核\n",
    "model.add(MaxPooling2D(pool_size=(2,2)))  \n",
    "#平铺层: 将数据形状转为向量\n",
    "model.add(Flatten())\n",
    "#全连接层: 隐藏层维度为256, 使用ReLU作为激活函数\n",
    "model.add(Dense(256, activation=\"relu\"))\n",
    "#全连接层: 隐藏层维度为10, 使用Softmax作为激活函数, 输出每个分类的概率\n",
    "model.add(Dense(10,activation=\"softmax\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d (Conv2D)              (None, 24, 24, 32)        832       \n",
      "_________________________________________________________________\n",
      "max_pooling2d (MaxPooling2D) (None, 12, 12, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_1 (Conv2D)            (None, 10, 10, 32)        9248      \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (None, 5, 5, 32)          0         \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 800)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 256)               205056    \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                2570      \n",
      "=================================================================\n",
      "Total params: 217,706\n",
      "Trainable params: 217,706\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#优化器\n",
    "#Adam优化器, 损失函数:分类交叉熵\n",
    "#度量指标: 准确率\n",
    "model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 3. 开始训练:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pre fit:1635768482.0415277\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-11-01 20:08:02.353457: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "2500/2500 [==============================] - 59s 23ms/step - loss: 0.1173 - accuracy: 0.9645 - val_loss: 0.0463 - val_accuracy: 0.9846\n",
      "Epoch 2/5\n",
      "2500/2500 [==============================] - 57s 23ms/step - loss: 0.0406 - accuracy: 0.9876 - val_loss: 0.0411 - val_accuracy: 0.9861\n",
      "Epoch 3/5\n",
      "2500/2500 [==============================] - 57s 23ms/step - loss: 0.0285 - accuracy: 0.9912 - val_loss: 0.0305 - val_accuracy: 0.9901\n",
      "Epoch 4/5\n",
      "2500/2500 [==============================] - 57s 23ms/step - loss: 0.0204 - accuracy: 0.9938 - val_loss: 0.0366 - val_accuracy: 0.9874\n",
      "Epoch 5/5\n",
      "2500/2500 [==============================] - 57s 23ms/step - loss: 0.0163 - accuracy: 0.9947 - val_loss: 0.0343 - val_accuracy: 0.9908\n",
      "fit done:1635768769.2069855\n",
      "duration:287.1654577255249\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential      #线性模型框架\n",
    "from keras.layers import Dense, Dropout, Flatten   #Dense全连接层; Flatten平铺层\n",
    "from keras.layers import Conv2D, MaxPooling2D   ##开始训练\n",
    "import time\n",
    "start_time=time.time()\n",
    "print(\"pre fit:\" + str(start_time))\n",
    "# time.sleep(2)\n",
    "model.fit(x_train, y_train, batch_size=24, epochs=5, validation_data=(x_test, y_test))\n",
    "end_time=time.time()\n",
    "print(\"fit done:\"+str(end_time))\n",
    "print(\"duration:\" + str(end_time-start_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "313/313 [==============================] - 2s 7ms/step - loss: 0.0343 - accuracy: 0.9908\n",
      "损失为0.034259\n",
      "准确度为0.990800\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(x_test, y_test)\n",
    "print(\"损失为%f\" % score[0])\n",
    "print(\"准确度为%f\" % score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.32941176]\n",
      "   [0.7254902 ]\n",
      "   [0.62352941]\n",
      "   [0.59215686]\n",
      "   [0.23529412]\n",
      "   [0.14117647]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.87058824]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.94509804]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.77647059]\n",
      "   [0.66666667]\n",
      "   [0.20392157]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.2627451 ]\n",
      "   [0.44705882]\n",
      "   [0.28235294]\n",
      "   [0.44705882]\n",
      "   [0.63921569]\n",
      "   [0.89019608]\n",
      "   [0.99607843]\n",
      "   [0.88235294]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.98039216]\n",
      "   [0.89803922]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.54901961]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.06666667]\n",
      "   [0.25882353]\n",
      "   [0.05490196]\n",
      "   [0.2627451 ]\n",
      "   [0.2627451 ]\n",
      "   [0.2627451 ]\n",
      "   [0.23137255]\n",
      "   [0.08235294]\n",
      "   [0.9254902 ]\n",
      "   [0.99607843]\n",
      "   [0.41568627]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.3254902 ]\n",
      "   [0.99215686]\n",
      "   [0.81960784]\n",
      "   [0.07058824]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.08627451]\n",
      "   [0.91372549]\n",
      "   [1.        ]\n",
      "   [0.3254902 ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.50588235]\n",
      "   [0.99607843]\n",
      "   [0.93333333]\n",
      "   [0.17254902]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.23137255]\n",
      "   [0.97647059]\n",
      "   [0.99607843]\n",
      "   [0.24313725]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.52156863]\n",
      "   [0.99607843]\n",
      "   [0.73333333]\n",
      "   [0.01960784]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.03529412]\n",
      "   [0.80392157]\n",
      "   [0.97254902]\n",
      "   [0.22745098]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.49411765]\n",
      "   [0.99607843]\n",
      "   [0.71372549]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.29411765]\n",
      "   [0.98431373]\n",
      "   [0.94117647]\n",
      "   [0.22352941]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.0745098 ]\n",
      "   [0.86666667]\n",
      "   [0.99607843]\n",
      "   [0.65098039]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.01176471]\n",
      "   [0.79607843]\n",
      "   [0.99607843]\n",
      "   [0.85882353]\n",
      "   [0.1372549 ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.14901961]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.30196078]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.12156863]\n",
      "   [0.87843137]\n",
      "   [0.99607843]\n",
      "   [0.45098039]\n",
      "   [0.00392157]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.52156863]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.20392157]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.23921569]\n",
      "   [0.94901961]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.20392157]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.4745098 ]\n",
      "   [0.99607843]\n",
      "   [0.99607843]\n",
      "   [0.85882353]\n",
      "   [0.15686275]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.4745098 ]\n",
      "   [0.99607843]\n",
      "   [0.81176471]\n",
      "   [0.07058824]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]\n",
      "\n",
      "  [[0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]\n",
      "   [0.        ]]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1.9872483e-10, 3.2242509e-10, 8.3694779e-08, 1.9647578e-08,\n",
       "        9.3434804e-10, 8.0260402e-11, 1.1132695e-12, 9.9999988e-01,\n",
       "        2.5343967e-09, 6.4106138e-09]], dtype=float32)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "# print(x_test[0:1])\n",
    "model.predict(x_test[0:1])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 4. 预测:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(351, 296)\n",
      "(28, 28)\n",
      "识别结果:5\n"
     ]
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.image as img\n",
    "\n",
    "# import glob\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "input_path=\"test/5.jpg\"\n",
    "# input_path=\"test/3_out_sm.jpg\"\n",
    "# input_path=\"test/3_out_gray.bmp\"\n",
    "im=Image.open(input_path)\n",
    "print(im.size)\n",
    "im=im.resize((28,28))  # 转换成28X28的尺寸\n",
    "print(im.size)\n",
    "\n",
    "im_1=im.convert(\"L\")\n",
    "im_data=im_1.getdata()\n",
    "im_array = np.array(list(im_data))\n",
    "im_array= im_array.reshape(28,28)\n",
    "im_array=255-im_array\n",
    "im_array=im_array.reshape(1,28,28,1)\n",
    "\n",
    "predict_result = model.predict(im_array)\n",
    "print(\"识别结果:%d\" % np.argmax(predict_result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
